package com.yoki.cloud.common.utils.logger;

import java.io.IOException;
import java.io.OutputStream;

public class SysColorPrinter {

    /*private static final String white = "\033[30m";
    private static final String red = "\033[31m";
    private static final String green = "\033[32m";
    private static final String yellow = "\033[33m";
    private static final String blue = "\033[34m";
    private static final String purple = "\033[35m";
    private static final String lightBlue = "\033[36m";
    private static final String gray = "\033[37m";

    public static void print(String str) {
        print(System.out, str);
    }

    public static void println(String str) {
        println(System.out, str);
    }

    public static void print(OutputStream out, String str) {
        try {
            out.write(str.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void println(OutputStream out, String str) {
        try {
            out.write(str.getBytes());
            out.write(System.lineSeparator().getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

    /*public static SysColorPrinter build(String str) {
        SysColorPrinter text = new SysColorPrinter(str, null, null);
        return text;
    }

    public static SysColorPrinter build(String str, TextStyle style) {
        SysColorPrinter text = new SysColorPrinter(str, null, style);
        return text;
    }

    public static SysColorPrinter build(String str, TextColor color) {
        SysColorPrinter text = new SysColorPrinter(str, color, null);
        return text;
    }

    public static SysColorPrinter build(String str, TextColor color, TextStyle style) {
        SysColorPrinter text = new SysColorPrinter(str, color, style);
        return text;
    }*/


    /**
     * 内部类，用于生产带格式的字符串
     */

    /**
     * 用于存储字符串
     */
//    private StringBuffer buffer = null;

    /*private SysColorPrinter() {
        buffer = new StringBuffer();
    }

    private SysColorPrinter(String str) {
        this(str, null, null);
    }

    private SysColorPrinter(StringBuffer buffer) {
        this(buffer.toString(), null, null);
    }

    private SysColorPrinter(StringBuffer buffer, TextColor color) {
        this(buffer.toString(), color, null);
    }

    private SysColorPrinter(String str, TextColor color) {
        this(str, color, null);
    }

    private SysColorPrinter(StringBuffer buffer, TextStyle style) {
        this(buffer.toString(), null, style);
    }

    private SysColorPrinter(String str, TextStyle style) {
        this(str, null, style);
    }

    private SysColorPrinter(StringBuffer buffer, TextColor color, TextStyle style) {
        this(buffer.toString(), color, style);
    }

    private SysColorPrinter(String str, TextColor color, TextStyle style) {
        this.buffer = new StringBuffer();
        appand(str, color, style);
    }*/

    /*public SysColorPrinter appand(String str) {
        return appand(str, null, null);
    }

    public SysColorPrinter appand(StringBuffer buffer) {
        return appand(buffer, null, null);
    }

    public SysColorPrinter appand(String str, TextColor color) {
        return appand(str, color, null);
    }

    public SysColorPrinter appand(StringBuffer buffer, TextColor color) {
        return appand(buffer, color, null);
    }

    public SysColorPrinter appand(String str, TextStyle style) {
        return appand(str, null, style);
    }

    public SysColorPrinter appand(StringBuffer buffer, TextStyle style) {
        return appand(buffer, null, style);
    }

    public SysColorPrinter appand(StringBuffer buffer, TextColor color, TextStyle style) {
        return appand(buffer.toString(), color, style);
    }

    public SysColorPrinter appand(String str, TextColor color, TextStyle style) {
        _append(str, color, style);
        return this;
    }*/

    public SysColorPrinter print(String str) {
        print(System.out, str, null, null);
        return this;
    }

    public SysColorPrinter print(String str, TextStyle style) {
        print(System.out, str, null, style);
        return this;
    }

    public SysColorPrinter print(String str, TextColor color) {
        print(System.out, str, color, null);
        return this;
    }

    public SysColorPrinter print(String str, TextColor color, TextStyle style) {
        print(System.out, str, color, style);
        return this;
    }

    public SysColorPrinter println(String str) {
        println(System.out, str, null, null);
        return this;
    }

    public SysColorPrinter println(String str, TextStyle style) {
        println(System.out, str, null, style);
        return this;
    }

    public SysColorPrinter println(String str, TextColor color) {
        println(System.out, str, color, null);
        return this;
    }

    public SysColorPrinter println(String str, TextColor color, TextStyle style) {
        println(System.out, str, color, style);
        return this;
    }

    public SysColorPrinter println(OutputStream out, String str) {
        print(out, str + System.lineSeparator(), null, null);
        return this;
    }

    public SysColorPrinter println(OutputStream out, String str, TextStyle style) {
        print(out, str + System.lineSeparator(), null, style);
        return this;
    }

    public SysColorPrinter println(OutputStream out, String str, TextColor color) {
        print(out, str + System.lineSeparator(), color, null);
        return this;
    }

    public SysColorPrinter println(OutputStream out, String str, TextColor color, TextStyle style) {
        print(out, str + System.lineSeparator(), color, style);
        return this;
    }

    public SysColorPrinter print(OutputStream out, String str) {
        try {
            String newStr = _build(str, null, null);
            out.write(newStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    public SysColorPrinter print(OutputStream out, String str, TextStyle style) {
        try {
            String newStr = _build(str, null, style);
            out.write(newStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    public SysColorPrinter print(OutputStream out, String str, TextColor color) {
        try {
            String newStr = _build(str, color, null);
            out.write(newStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }

    public SysColorPrinter print(OutputStream out, String str, TextColor color, TextStyle style) {
        try {
            if (null == out) {
                throw new NullPointerException();
            }
            String newStr = _build(str, color, style);
            out.write(newStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    private String _build(String str, TextColor color, TextStyle style) {
        if (null == str || str.equals("")) {
            return str;
        }
        StringBuffer buf = new StringBuffer();
        if (null != color) {
            buf.append(color.getValue().toString());
        }
        if (null != style) {
            buf.append(style.getValue().toString());
        }
        buf.append(str.toString());
        //设置完前面的格式后，清除后面格式
        buf.append(TextStyle.empty.getValue().toString());
        return buf.toString();
    }

}


